// pages/app/schools/schools.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    list: []
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
  },
  onShow() {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/school/list';
    var params = {};
    var success = function(res) {
      var list = res.info.list;
      if (!wx.getStorageSync("user")) {
        for (var i = 0; i < list.length; i ++) {
          list[i].userId = null;
        }
      }
      self.setData({
        list: list
      })
    }
    var fail = function() {};
    var complete = function() {};
    utils.request(url, params, success, fail, complete);
  },
  subscribe(e) {
    if (!wx.getStorageSync("user")) {
      // wx.showToast({
      //   title: '请先登录',
      //   icon: 'none'
      // })
      wx.checkSession({
        success() {
          app.login(self.route);
        },
        fail() {
          // app.loginWechat(app.login(self.route));
          app.globalData.openId = null;
          app.serverLogin({
            success() {
              console.log('自动登录成功')
            },
            fail() {
              console.log('自动登录失败')
            }
          })
        }
      })
      return;
    }
    var schoolId = e.currentTarget.dataset.schoolid;
    var url = host + '/lx/mini/school/subscribe';
    var params = {
      schoolId: schoolId
    };
    var success = function (res) {
      wx.showToast({
        title: '已订阅',
        icon: 'success'
      })
      var list = self.data.list;
      for (var i = 0; i < list.length; i ++) {
        if (list[i].id == schoolId) {
          list[i].userId = "0";
          break;
        }
      }
      self.setData({
        list: list
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  unsubscribe(e) {
    if (!wx.getStorageSync("user")) {
      // wx.showToast({
      //   title: '请先登录',
      //   icon: 'none'
      // })
      wx.checkSession({
        success() {
          app.login(self.route);
        },
        fail() {
          // app.loginWechat(app.login(self.route));
          app.globalData.openId = null;
          app.serverLogin({
            success() {
              console.log('自动登录成功')
            },
            fail() {
              console.log('自动登录失败')
            }
          })
        }
      })
      return;
    }
    var schoolId = e.currentTarget.dataset.schoolid;
    var url = host + '/lx/mini/school/unsubscribe';
    var params = {
      schoolId: schoolId
    };
    var success = function (res) {
      wx.showToast({
        title: '已取消订阅',
        icon: 'success'
      })
      var list = self.data.list;
      for (var i = 0; i < list.length; i++) {
        if (list[i].id == schoolId) {
          list[i].userId = null;
          break;
        }
      }
      self.setData({
        list: list
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  goInfo(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/schoolHome/schoolHome?schoolId=' + id
    })
  },
  goMajors(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/majors/majors?schoolId=' + id
    })
  },
  goPolicies(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/policy/policy?schoolId=' + id
    })
  }
})